package com.secret;



import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


// 可以看出对于特定的字符串生成的md5是不变的.
public class javaMD5 {

	
	
	
	
	// AES  . key为秘钥, content为要加密的内容
    private static byte[] encrypt(byte[] key, byte[] content) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
//        Cipher cipher = Cipher.getInstance("AES");
         Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); //php默认
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
                new byte[cipher.getBlockSize()]));
        byte[] encrypted = cipher.doFinal(content); // content
        
        return encrypted; // 已加密的内容
    }
    
    
    public static byte[] decrypt( byte[] key, byte[] content ) throws Exception {
    	
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
//      Cipher cipher = Cipher.getInstance("AES");
       Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); //php默认
      cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
              new byte[cipher.getBlockSize()]));
      byte[] encrypted = cipher.doFinal(content); // content
      
      return encrypted; // 已解密的内容
    }
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String url ="www.baidu.com";
		String ip ="1.2.3.4";
		
		 MessageDigest digest = null;
		 
	        try {
	            digest = MessageDigest.getInstance("MD5");
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        
	        String simple ="123456";
	       digest.update( simple.getBytes() );
	       System.out.println( byteToHexString(digest.digest()) );
	        
	        
	        if (null != digest) {
	        	
	            digest.update((getURI(url)+ip).getBytes());
	      
	            byte[] bDgiest = digest.digest();
	            System.out.println("Raw key md5 is:\t" +getURI(url)+ip +"->"+ byteToHexString(bDgiest));
	            for (int i = 0; i < 8; i++) {
	                bDgiest[i] ^= bDgiest [i+8];
	            }
	        }
	        
	        
	        /////////////// 
	        String aes_key ="12345678"+"12345678"; //16bytes
	        System.out.println("开始aes加密");
	        
	     //   byte[] encode =null;
	        try {
	        	byte[] encode = encrypt(aes_key.getBytes(), "hello world12345".getBytes() );
	        	byte[] code = decrypt(aes_key.getBytes(), encode );
				
	        	
	        	System.out.println( new String(code) );
	        	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	            
	          
	     } // end main 
	

	  static String getURI(String s){
		  return s;
	  }
	  
	  
	  private static String byteToString(byte[] data){
		  
		  return new String(data);
	  }
	  
		private static String byteToHexString(byte[] data) {
	    	if(data == null) {
	    		return null;
	    	}
	    	
	    	
	        String s;
	        String str = "";
	     //   Log.i("Leng", ""+data.length);
	        for (int i = 0; i < data.length; i++) {
	            s = Integer.toHexString(0xFF & data[i]).toUpperCase();
	            str += s.length() == 1 ? "0" + s : s; 
	        }
	        return str;
	    }	  
	  
	        
}
